import { login, logout, getInfo, teacherRegister, examineeRegister, changePassword, xwTeacherRegister, findPassword } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    roleId: '',
    teacherTypeCode: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_ROLEID: (state, roleId) => {
      state.roleId = roleId
    },
    SET_TEACHERTYPE: (state, teacherTypeCode) => {
      state.teacherTypeCode = teacherTypeCode
    }

  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {
          const data = response
          // console.log('data:' + data.userId)
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          resolve()
        }).catch(error => {
          console.log(error)
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token).then(response => {
          const data = response
          if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data.roles)
            commit('SET_ROLEID', data.roleId)
          } else {
            reject('没有获取到你的角色信息，请联系管理员!')
          }
          commit('SET_NAME', data.name)
          commit('SET_TEACHERTYPE', data.teacherTypeCode)
          if (data.gender === '1') {
            commit('SET_AVATAR', './static/man.png')
          } else {
            commit('SET_AVATAR', './static/girl.png')
          }
          resolve(response)
        }).catch(() => {
          reject('session过期，请重新登陆')
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_ROLEID', '')
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    // 用户注册
    TeacherRegister({ commit }, registerForm) {
      // console.log(registerForm)
      const username = registerForm.username
      const password = registerForm.password
      const mobile = registerForm.mobile
      return new Promise((resolve, reject) => {
        teacherRegister(username, password, mobile).then(response => {
          // console.log(response)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    XwTeacherRegister({ commit }, registerForm) {
      const username = registerForm.username
      const name = registerForm.name
      const birthday = registerForm.birthday
      const gender = registerForm.gender
      const mobilePhone = registerForm.mobilePhone
      const email = registerForm.email
      const password = registerForm.password
      return new Promise((resolve, reject) => {
        xwTeacherRegister(username, name, birthday, gender, mobilePhone, email, password).then(response => {
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    ExamineeRegister({ commit }, registerForm) {
      // console.log(registerForm)
      const username = registerForm.username
      const password = registerForm.password
      return new Promise((resolve, reject) => {
        examineeRegister(username, password).then(response => {
          // console.log(response)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    ChangePassword({ commit }, form) {
      const username = form.username
      const oldPassword = form.oldPassword
      const newPassword = form.newPassword
      return new Promise((resolve, reject) => {
        changePassword(username, oldPassword, newPassword).then(response => {
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    FindPassword({ commit }, form) {
      const username = form.username
      const mobile = form.mobile
      const newPassword = form.newPassword
      return new Promise((resolve, reject) => {
        findPassword(username, mobile, newPassword).then(response => {
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
